package cn.tedu.straw.api.user.controller;

import cn.tedu.straw.api.user.dto.StudentRegisterDTO;
import cn.tedu.straw.api.user.ex.ParamValidationException;
import cn.tedu.straw.api.user.service.IUserService;
import cn.tedu.straw.api.user.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired
    IUserService userService;

    // http://localhost:8081/portal/users/reg/student?inviteCode=JSD2006-888888&nickName=Tom&phone=13800138001&password=1234
    @RequestMapping("/reg/student")
    public R<Void> studentRegister(@Valid StudentRegisterDTO studentRegisterDTO,
                                   BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errorMessage);
        }

        userService.studentRegister(studentRegisterDTO);
        return R.ok();
    }

}
